import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex, requestFunHtml } from '@/utils';
import { useRequest } from 'umi';
import { getBaseUrl } from '@/services/urlconst';
import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';

const baseUrl = getBaseUrl();

/**
 * 在线编辑
 * @param callback 接口回调
 * @returns
 */
const saveToURLWordService = (callback: (res: any, clickType: boolean) => void) => {
  const saveToURL = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/saveToURL`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    wordSaveToURLRun: saveToURL?.run,
    wordSaveToURLLoading: saveToURL?.loading,
  };
};

/**
 * 添加合同
 * @param callback 接口回调
 * @returns
 */
const addContractService = (callback: (res: any, clickType: boolean) => void, addType: string) => {
  const addContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownerID: getUserID(),
        ownPositionID: getPosIDs(),
        ownDepartmentID: getUserDepID(),
        ownCompanyID: getCompanyID(),
        // dutyerID: getUserID(),
        // dutyPositionID: getPosIDs(),
        // dutyDepartmentID: getUserDepID(),
        // dutyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/add`;
      if (addType == 'UpdateRepairAdd') {
        reqUrl = `${baseUrl}/contractController/addRecord`;
      }
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addContractRun: addContract?.run,
    addContractLoading: addContract?.loading,
  };
};

/**
 * 添加合同
 * @param callback 接口回调
 * @returns
 */
const addAndSubmitContractService = (callback: (res: any, clickType: boolean, isNeedSubmit: boolean) => void, addType: string) => {
  const addAndSubmitContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownerID: getUserID(),
        ownPositionID: getPosIDs(),
        ownDepartmentID: getUserDepID(),
        ownCompanyID: getCompanyID(),
        // dutyerID: getUserID(),
        // dutyPositionID: getPosIDs(),
        // dutyDepartmentID: getUserDepID(),
        // dutyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/add`;
      if (addType == 'UpdateRepairAdd') {
        reqUrl = `${baseUrl}/contractController/addRecord`;
      }
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true, true);
      },
    },
  );
  return {
    addAndSubmitContractRun: addAndSubmitContract?.run,
    addAndSubmitContractLoading: addAndSubmitContract?.loading,
  };
};

/**
 * 编辑合同
 * @param callback 接口回调
 * @returns
 */
const editContractService = (callback: (res: any, clickType: boolean) => void) => {
  const editContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editContractRun: editContract?.run,
    editContractLoading: editContract?.loading,
  };
};

/**
 * 编辑补录合同
 * @param callback 接口回调
 * @returns
 */
const editRecordContractService = (callback: (res: any, clickType: boolean) => void) => {
  const editRecordContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/editRecord`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editRecordContractRun: editRecordContract?.run,
    editRecordContractLoading: editRecordContract?.loading,
  };
};

/**
 * 提交补录合同
 * @param callback 接口回调
 * @returns
 */
const submitRecordContractService = (callback: (res: any, clickType: boolean) => void) => {
  const submitRecordContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/submitRecord`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    submitRecordContractRun: submitRecordContract?.run,
    submitRecordContractLoading: submitRecordContract?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getMcontractByIDService = (callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`, //采购类别
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //采购类型
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_METHOD}`, //采购方式
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}`, //合同状态
    `${getTenantID()}${ILoadMapTypeAll.PAYMENT_STYLE}`, //支付方式
    `${getTenantID()}${ILoadMapTypeAll.COMPANY_TAXRATE}`, //税率
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_FIELDTYPE}`, //合同附件类型
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`, //量化单位
    `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
  ];

  const getMcontractByID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/contractController/getMcontractByID`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'state,supplyKind,supplyType,procureReal,payStyle,businessType,checkState,orderType,procureOrg',
        orgTransName: `${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT},${ISearchTranslateAll.DUTY_DEPT},busiDepartmentID,relateDepartmentID,contacterID`,
        currentUserID: getUserID(),
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getMcontractByIDRun: getMcontractByID?.run,
    getMcontractByIDLoading: getMcontractByID?.loading,
    getMcontractByIDInfo: getMcontractByID?.data?.obj,
    reloadDetailInfo: getMcontractByID?.refresh,
  };
};

/**
 * 添加相对方
 * @param callback 接口回调
 * @returns
 */
const addCounterpartService = (callback: (res: any, clickType: boolean) => void) => {
  // const baseUrl = "http://192.168.153.192:31019"
  const addCounterpart = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierInfoController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addCounterpartRun: addCounterpart?.run,
    addCounterpartLoading: addCounterpart?.loading,
  };
};

/**
 * 上传电子扫描
 * @param callback 接口回调
 * @returns
 */
const signContractService = (callback: (res: any) => void) => {
  const signContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        ownerID: getUserID(),
        ownPositionID: getPosIDs(),
        ownDepartmentID: getUserDepID(),
        ownCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/sign`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    signContractRun: signContract?.run,
    signContractLoading: signContract?.loading,
  };
};

/**
 * 复制范本路劲
 * @param callback 接口回调
 * @returns
 */
const copyTemplateService = (callback: (res: any) => void) => {
  const copyTemplate = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/copyTemplate`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    copyTemplateRun: copyTemplate?.run,
    copyTemplateLoading: copyTemplate?.loading,
  };
};

/**
 * 合同签章
 * @param callback 接口回调
 * @returns
 */
const sealContractService = (callback: (res: any) => void) => {
  const sealContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        signCompanyID: getCompanyID(),
        signDepartmentID: getUserDepID(),
        signPositionID: getPosIDs(),
        signerID: getUserID(),
      };
      let reqUrl = `${baseUrl}/contractController/seal`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    sealContractRun: sealContract?.run,
    sealContractLoading: sealContract?.loading,
  };
};

/**
 * 合同删除
 * @param callback 接口回调
 * @returns
 */
const deleteContractService = (callback: (res: any) => void) => {
  const deleteContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/delete`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    deleteContractRun: deleteContract?.run,
    deleteContractLoading: deleteContract?.loading,
  };
};

/**
 * 合同撤销
 * @param callback 接口回调
 * @returns
 */
const invalidContractService = (callback: (res: any) => void) => {
  const invalidContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/invalid`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    invalidContractRun: invalidContract?.run,
    invalidContractLoading: invalidContract?.loading,
  };
};

/**
 * 获取履约记录
 * @param callback 详情回调
 * @returns
 */
const getMcontractProcessService = (callback: (res: any) => void) => {
  const getMcontractProcess = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/getMcontractPayList`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    getMcontractProcessRun: getMcontractProcess?.run,
    getMcontractProcessLoading: getMcontractProcess?.loading,
    getMcontractProcessData: getMcontractProcess?.data?.obj,
  };
};

/**
 * 获取节点记录
 * @param callback 详情回调
 * @returns
 */
const getMcontractProcessListService = (callback: (res: any) => void) => {
  const getMcontractProcessList = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        orgTransName: `${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.CANCEL_DEPT},${ISearchTranslateAll.MODIFY_DEPT}`,
        sort: 'createTime',
        order: 'desc',
      };
      let reqUrl = `${baseUrl}/contractController/getMcontractProcessList`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    getMcontractProcessListRun: getMcontractProcessList?.run,
    getMcontractProcessListLoading: getMcontractProcessList?.loading,
    getMcontractProcessListData: getMcontractProcessList?.data?.obj,
  };
};

/**
 * 履约完成
 * @param callback 详情回调
 * @returns
 */
const finishProcessService = (callback: (res: any) => void) => {
  const finishProcess = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyCompanyID: getCompanyID(),
        modifyDepartmentID: getUserDepID(),
        modifyPositionID: getPosIDs(),
        modifyerID: getUserID(),
      };
      let reqUrl = `${baseUrl}/contractController/finishProcess`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    finishProcessRun: finishProcess?.run,
    finishProcessLoading: finishProcess?.loading,
    // finishProcessData: finishProcess?.data?.obj,
  };
};

/**
 * 一键督办
 * @param callback
 * @returns
 */
const batchHandleService = (callback: (res: any) => void) => {
  const batchHandle = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // modifyCompanyID: getCompanyID(),
        // modifyDepartmentID: getUserDepID(),
        // modifyPositionID: getPosIDs(),
        // modifyerID: getUserID(),
      };
      let reqUrl = `${baseUrl}/contractController/batchHandle`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    batchHandleRun: batchHandle?.run,
    batchHandleLoading: batchHandle?.loading,
    // batchHandleData: batchHandle?.data?.obj,
  };
};

/**
 * 导出
 * @param callback
 * @returns
 */
const exportMcontractService = (callback: (res: any) => void) => {
  const exportMcontract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        pageSize: 500000,
      };
      let reqUrl = `${baseUrl}/contractController/exportMcontract/${bodyParams?.params}`;
      return requestFunHtml('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    exportMcontractRun: exportMcontract?.run,
    exportMcontractLoading: exportMcontract?.loading,
  };
};

/**
 * 添加履约关键节点
 * @param callback
 * @returns
 */
const addProcessService = (callback: (res: any) => void) => {
  const addProcess = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/addProcess`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    addProcessRun: addProcess?.run,
    addProcessLoading: addProcess?.loading,
  };
};

/**
 * 合同归档
 * @param callback 接口回调
 * @returns
 */
const finishContractService = (callback: (res: any) => void) => {
  const finishContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/finish`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    finishContractRun: finishContract?.run,
    finishContractLoading: finishContract?.loading,
  };
};

/**
 * 添加附件
 * @param callback 接口回调
 * @returns
 */
const addContractAttachService = () => {
  const addAttachContract = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownerID: getUserID(),
        ownPositionID: getPosIDs(),
        ownDepartmentID: getUserDepID(),
        ownCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractController/refreshAttach`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {},
    },
  );
  return {
    addContractAttachRun: addAttachContract?.run,
    addContractAttachLoading: addAttachContract?.loading,
  };
};

export {
  saveToURLWordService,
  addContractService,
  addCounterpartService,
  editContractService,
  getMcontractByIDService,
  signContractService,
  copyTemplateService,
  sealContractService,
  deleteContractService,
  invalidContractService,
  getMcontractProcessService,
  getMcontractProcessListService,
  finishProcessService,
  batchHandleService,
  exportMcontractService,
  addProcessService,
  submitRecordContractService,
  editRecordContractService,
  finishContractService,
  addContractAttachService,
  addAndSubmitContractService,
};
